package com.way.demo.sort;

import com.way.demo.SpringbootDemoApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;

/**
 * @author lipingwei
 * @create 2021-07-07 9:19
 */
@SpringBootTest(classes = SpringbootDemoApplication.class)
@RunWith(SpringRunner.class)
public class SortTest2 {
    @Test
    public void inserSort(){
        int[] arr={3,4,1,2,5};
         for(int i=1;i<arr.length;i++){
            int temp=arr[i];
            int index=i-1;
            while(index>=0&&temp<arr[index]){
                arr[index+1]=arr[index];
                index--;
            }
            arr[index+1]=temp;
        }
        System.out.println(Arrays.toString(arr));
    }
    @Test
    public void inserSort2(){
        int[] arr={3,4,1,2,5};
        for(int i=1;i<arr.length;i++){
            for(int j=i;j>0;j--){
                if(arr[j-1]>arr[j]){
                    arr[j-1]^=arr[j];
                    arr[j]^=arr[j-1];
                    arr[j-1]^=arr[j];
                }else {
                    break;
                }
            }
        }
        System.out.println(Arrays.toString(arr));

    }

}
